package newx
{	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import newx.com.LayoutType;
		
	public class StageInfo
	{
		/**版本*/		
		public	static	var version			:String				=	"Debug - 0.0.1";
		
		public static	var	isInit			:Boolean			=	false;
		/**
		 * 浏览器附加参数,
		 * 包含名称-值对的对象，表示为被加载的 SWF 文件提供的参数。 
		 * 可以使用 for-in 循环来提取 parameters 对象中的所有名称和值。
		 */
		public	static	var params				:Object				=	new Object();
		
//		public static	var luaPath				:String				=	"config/logic/";
		
		public static	var sceneResPath		:String				=	"res/scenes/";
		
		public static	var npcResPath			:String				=	"res/actors/";
		
		public static	var swfResPath			:String				=	"res/swf/";
		
		public static	var pngResPath			:String				=	"res/png/";
		
		public static	var jpgResPath			:String				=	"res/jpg/";
		
		public static	var iconResPath			:String				=	"res/items/";
		
		public static var loadingbar			:DisplayObject;
		
		/**用于显示版本信息的文本*/
		private	static	var tLabel			:TextField			=	new TextField();
		
		/**主舞台横向宽度*/
		public	static	var rootW				:Number				=	0;
		
		/**主舞台纵向高度*/
		public	static	var rootH				:Number				=	0;
		
		/**主舞台横向宽度*/
		public	static	var rootMaxW			:Number				=	0;
		
		/**主舞台纵向高度*/
		public	static	var rootMaxH			:Number				=	0;
		
		/**舞台对象*/
		public	static	var	stage				:Stage;
		
		/**主背景层*/
		private	static	var	mainBGSpace		:Sprite;
				
		/**主容器层,所有游戏容器添加到此容器下级*******************************/
		public	static	var	mainSpace			:Sprite			=	new Sprite();
		
		/**扩展层*/
		public	static	var	defaultSpace		:Sprite			=	new Sprite()
		
		/**系统窗口层*/
		public	static	var	systemSpace			:Sprite			=	new Sprite();
		
		/**新手引导层***/
		public static var guideSpace			:Sprite			=	new Sprite();
		
		public  static var storyMoveSpace		:Sprite			=	new Sprite();
		
		/**ui层*/
		public	static	var	uiSpace				:Sprite			=	new Sprite();
		
		/**特效层*/
		public	static	var	effectSpace			:Sprite			=	new Sprite();
				
		/**场景层(背景)*/
		public	static	 var	sceneSpace		:Sprite			=	new Sprite();
		
		/**战斗层**/
		public static var	attackSpace			:Sprite			=	new Sprite();
		
		//=================================================================
		private static	var 	bgBitmapData	:BitmapData;
		
		private static	var		_layout			:String;
		
		
		public static function setLayout(layout : String):void
		{
			if(_layout == layout) return;
			
			_layout	=	layout;
			
			onStageReSize();
		}
		
		public	static	function Clear(notRemoves:Array = null):void
		{
			var length	:uint		=	stage.numChildren;
			var tArr	:Array		=	new Array();
			var tObj	:DisplayObject;
			
			var notSize	:uint		=	0;
			var isNot	:Boolean	=	false;
			
			if(notRemoves != null)
			{
				notSize	=	notRemoves.length;
			}
			
			for(var i:uint = 0 ; i < length ; i ++)
			{
				tObj	=	stage.getChildAt(i);
				if(tObj != mainSpace && tObj != mainBGSpace)
				{
					isNot	=	false;
					for(var j:uint = 0 ; j < notSize ; j ++)
					{
						if(notRemoves[j] == tObj)
						{
							isNot	=	true;
						}
					}
					if(!isNot)
					{
						tArr.push(tObj);
					}					
				}
			}
			
			while(tArr.length>0)
			{
				tObj	=	tArr.pop() as DisplayObject;
				stage.removeChild(tObj);
				tObj	=	null;
			}
		}
		
		public	static	function	InitStage(
			_stage		:Stage,
			_width		:uint,
			_height		:uint,
			_widthMax	:uint,
			_heightMax	:uint,
			bgResUrl	:*		=	null,
			layout		:String = 	null):void
		{
			if(isInit)
			{
				return;
			}
			//记录主舞台==============================================================================
			stage	=	_stage;
			stage.quality = StageQuality.HIGH;
			
			if(layout == null)
			{
				layout	=	LayoutType.TILE;
			}
			
			if(stage ==	null)
			{
				throw new Error("Stage is null!");
				return;
			}
			
			rootW		=	_width;
			rootH		=	_height;
			rootMaxW	=	_widthMax;
			rootMaxH	=	_heightMax;
			_layout		=	layout;
			//设置舞台布局============================================================================
			stage.align		=	StageAlign.TOP_LEFT;
			stage.scaleMode	=	StageScaleMode.NO_SCALE;
			
			//设置舞台背景============================================================================
			onStageReSize();	
			var onImgLoadComplete:Function	=	function(e:Event):void
			{
				//bitmap e.data
				bgBitmapData	=	(e.target.content as Bitmap).bitmapData;
				mainBGSpace.graphics.clear();
				mainBGSpace.graphics.beginBitmapFill(bgBitmapData);
				mainBGSpace.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
				mainBGSpace.graphics.endFill();
				mainBGSpace.visible = false;
				
				onStageReSize();
			};
			
			var onImgAddComplete:Function	=	function(e:Event):void
			{
				mainBGSpace.addChild(e.target.content);
				mainBGSpace.x	=	(stage.stageWidth	-mainBGSpace.width)*.5
				mainBGSpace.y	=	(stage.stageHeight	-mainBGSpace.height)*.5
			};
			
			var tLoader:Loader	=	new Loader();
			
			if(bgResUrl != "" && bgResUrl != null)
			{
				mainBGSpace	=	new Sprite();
				stage.addChild(mainBGSpace);
				
				if(bgResUrl is String)
				{
					if(layout == LayoutType.CENTER)
					{
						tLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
							onImgLoadComplete);						
					}
					else if(layout == LayoutType.TILE)
					{
						tLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
							onImgLoadComplete);
					}
					
					tLoader.load(new URLRequest(bgResUrl));
				}
				else
				{
					if(layout == LayoutType.CENTER)
					{
						mainBGSpace.addChild(bgResUrl);
						onImgAddComplete(bgResUrl)
					}
					else if(layout == LayoutType.TILE)
					{
						onImgLoadComplete(bgResUrl)
					}
				}
			}
			
			//设置容器============================================================================
								
			//主容器
			stage.addChild(mainSpace);
			
			//场景层6
			mainSpace.addChild(sceneSpace);
			
			//特效层5
			mainSpace.addChild(effectSpace);
			//ui层4
			mainSpace.addChild(uiSpace);
			//新手引导层
			mainSpace.addChild(guideSpace);
			//剧情层3
			mainSpace.addChild(storyMoveSpace);
			
			mainSpace.addChild(attackSpace);
			//系统窗口层2
			mainSpace.addChild(systemSpace);

			//扩展层1
			mainSpace.addChild(defaultSpace);
			defaultSpace.mouseChildren = defaultSpace.mouseEnabled = false;
			
			isInit	=	true;
			
			_stage.addEventListener(Event.RESIZE, onStageReSize, false, 10);
		}
		
		public static function set bgVisible(val:Boolean):void
		{
			mainBGSpace.visible	=	val;
		}
		
		/**重绘舞台*/
		private static function onStageReSize(evt : Event = null):void
		{
			if(bgBitmapData != null)
			{
				mainBGSpace.graphics.clear();
				mainBGSpace.graphics.beginBitmapFill(bgBitmapData);
				mainBGSpace.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
				mainBGSpace.graphics.endFill();
			}
			
			if(_layout == LayoutType.TILE )
			{
				if(rootW <= 0 || stage.stageWidth < rootW)
				{//<1000
					//此时不定位
					mainSpace.x	=	0;
				}
				else if(stage.stageWidth < rootMaxW)
				{//1000-1790,1000来居中
					//超出最大范围，默认剧中
					mainSpace.x	=	(stage.stageWidth-rootW)*.5;
				}else
				{
					mainSpace.x	=	(stage.stageWidth-rootW)*.5 ;
				}
				
				//>1790
				if(rootH <= 0 || stage.stageHeight < rootH)
				{
					mainSpace.y	=	0;
				}
				else if(rootH <= stage.stageHeight && stage.stageHeight < rootMaxH)
				{
					mainSpace.y	=	stage.stageHeight-rootH;
				}
				else
				{
					mainSpace.y	=	stage.stageHeight-rootH  - (stage.stageHeight-rootMaxH)*.5;
				}
			}
			else if(_layout == LayoutType.CENTER )
			{
				mainSpace.x	=	(stage.stageWidth-rootW)*.5;
				mainSpace.y	=	(stage.stageHeight-rootH)*.5;
			}
		}
	}
}